﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TTCaller
{
    public partial class CallerLoginPanel : UserControl
    {

        public CallerLoginPanel()
        {
            InitializeComponent();
            nLoc = 0;
            button_Type1_Click(null, null);
        }

        string m_strLastData = "";

        public void initial()
        {
            LogManager.getInstance().Log("CLP init:");
            SoundManager.getInstance().m_bPlayWelcome = false;
            nLoc = 0;
            button_Type1_Click(null, null);

            
            string strResult = StoreData.getInstance().TEL;
            this.textBox_Data.Text = "";
            for (int i = 0; i < strResult.Length; i++)
            {
                int n = strHalf.IndexOf(strResult[i]);
                this.textBox_Data.Text += strFull[n];
            }

            this.label_Title.Text = StoreData.getInstance().TTTitle;
            this.label_Tel.Text = "客服專線: " + StoreData.getInstance().TTTEL;





        }

        private void button_Input_Click(object sender, EventArgs e)
        {



            if (this.textBox_Data.Text.Length >= 11)
                return;
            string str = ((Button)sender).Text;

            if (this.textBox_Data.Text == "９９９８" && str == "Ｂ")
            {
                Form_Main.Main.button_Log_Click(null, null);
                return;


            }
            this.textBox_Data.Text += str;

        }

        private void button_Login_Clear_Click(object sender, EventArgs e)
        {
            this.textBox_Data.Text = "";

        }
        string strFull = "０１２３４５６７８９ＱＷＥＲＴＹＵＩＯＰＡＳＤＦＧＨＪＫＬＺＸＣＶＢＮＭ＿";
        string strHalf =  "0123456789QWERTYUIOPASDFGHJKLZXCVBNM_" ;
        private void button_Confirm_Click(object sender, EventArgs e)
        {
            string strResult = "";
            for (int i = 0; i < this.textBox_Data.Text.Length; i++)
            {
                int n = strFull.IndexOf(this.textBox_Data.Text[i]);
                strResult += strHalf[n];
            }
            if (StoreData.getInstance().TEL != strResult)
            {
                StoreData.getInstance().TEL = strResult;
            }
            if (strResult.Length > 4)
            {
                string strANS = CmdTalker.getInstance().SendServerAndWaitResponse("ST"+strResult, 8, "SA", true);

                if (strANS.IndexOf("SA") == -1 || strANS.Length < 3)
                {
                    LogManager.getInstance().Log("傳送失敗:" + "ST" + strResult + "~" + strANS);
                }
                else
                {
                    Form_Main.Main.m_strFuncControl = strANS.Substring(2, 1);
                    Form_Main.Main.m_strAddr = strANS.Substring(3, strANS.Length - 4);


                    SoundManager.getInstance().playWelcome();
                    ButtonControl(true);
                    Form_Main.Main.m_bLogined = true;
                    Form_Main.Main.ChangeToMainScreen();
                }
            }


        }

        public void ReLogin(string strANS)
        {
            LogManager.getInstance().Log("Relogin:" + "ST" + StoreData.getInstance().TEL + ":" + strANS );
            try
            {

                Form_Main.Main.m_strFuncControl = strANS.Substring(2, 1);
                Form_Main.Main.m_strAddr = strANS.Substring(3, strANS.Length - 4);
                ButtonControl(true);
                Form_Main.Main.m_bLogined = true;

                Form_Main.Main.ChangeToMainScreen();
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("Relogin Error", ex);

            }

        }

        private string getFullText(string arg_strHalf)
        {
            string strResult = "";
            for (int i = 0; i < arg_strHalf.Length; i++)
            {
                int n = strHalf.IndexOf(arg_strHalf[i]);
                strResult += strFull[n];
            }
            return strResult;

        }


        private void button_Type2_Click(object sender, EventArgs e)
        {
            nLoc = 0;
            button_Type1_Click(sender, e);
        }

        static public string ButtonText = "１２３４５６７８９０ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺ";

        int nLoc = 0;
        private void button_Type1_Click(object sender, EventArgs e)
        {
            this.button_Login_1.Text = getButtonText();
            this.button_Login_2.Text = getButtonText();
            this.button_Login_3.Text = getButtonText();
            this.button_Login_4.Text = getButtonText();
            this.button_Login_5.Text = getButtonText();
            this.button_Login_6.Text = getButtonText();
            this.button_Login_7.Text = getButtonText();
            this.button_Login_8.Text = getButtonText();
            this.button_Login_9.Text = getButtonText();
            this.button_Login_0.Text = getButtonText();
            this.button_Login_A.Text = getButtonText();
            this.button_Login_B.Text = getButtonText();

            if (nLoc > ButtonText.Length) nLoc = 0;
        }

        private string getButtonText()
        {
            if (nLoc > ButtonText.Length-1) nLoc = 0;
            return ButtonText.Substring(nLoc++, 1);

        }

        private void button_BS_Click(object sender, EventArgs e)
        {
            if(this.textBox_Data.Text.Length > 0)
                this.textBox_Data.Text = this.textBox_Data.Text.Remove(this.textBox_Data.Text.Length-1,1);
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            this.textBox_Data.Text = "";
        }


        private void ButtonControl(bool arg_bEnabled)
        {
            foreach (Control c in this.Controls)
            {
                if (c is Button)
                {
                    c.Enabled = arg_bEnabled;
                }
            }

        }



    }
}
